// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TurretControlAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Navigation.Runtime.Motion
{
    using System;
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Hardware.DAC;
    using Microsoft.Robotics.Hardware.Runtime.DAC;
    using Microsoft.Robotics.Runtime;

    /// <summary>
    /// Max's turret control agent. 
    /// </summary>
    [DataContract]
    public class TurretControlAgent : Agent, ISubscriptionReceiver<SetTurretPanMessage>
    {
        /// <summary>
        /// Name of the agent producing turret pan message
        /// </summary>
        [DataMember(Name = "TurretPanMessageProducer")]
        private string turretPanMessageProducer = default(string);

        /// <summary>
        /// Name of the heartbeat agent
        /// </summary>
        [DataMember(Name = "HeartbeatAgent")]
        private string heartbeatAgentName = default(string);        

        /// <summary>
        /// Turret pan message forwarder.
        /// </summary>
        private IForwarder<SetTurretPanMessage> turretPanControlPort;

        /// <summary>
        /// Agent producing a SetTurretPanMessage messages
        /// </summary>
        private IAgentHost turretPanMessageProducerAgent;

        /// <summary>
        /// Initializes a new instance of the TurretControlAgent class.
        /// </summary>
        /// <param name="name">Agent name.</param>
        /// <param name="producer">The name of the turret pan message producer agent.</param>
        /// <param name="heartbeatAgentName">The name of the Heartbeat agent to which we send control.</param>
        public TurretControlAgent(
            string name, 
            IProducer<SetTurretPanMessage> producer,
            IControllable<SetTurretPanMessage> heartbeatAgentName)
            : base(name)
        {
            this.turretPanMessageProducer = producer.Name;
            this.heartbeatAgentName = heartbeatAgentName.Name;
        }

        /// <summary>
        /// Initializes the TurretControlAgent class.
        /// </summary>
        /// <param name="locator">The locator to use for agent wiring.</param>
        public override void Initialize(AgentLocator locator)
        {
            if (this.turretPanMessageProducer == default(string) ||
                this.heartbeatAgentName == default(string))
            {
                throw new ArgumentException(string.Format("At least one required initialization parameter is missing for {0}.", this.GetType().FullName));
            }

            base.Initialize(locator);

            this.turretPanControlPort = locator.GetAgent(this.heartbeatAgentName).GetControlPort<SetTurretPanMessage>();

            this.turretPanMessageProducerAgent = locator.GetAgent(this.turretPanMessageProducer);
            this.turretPanMessageProducerAgent.Subscribe<SetTurretPanMessage>(this);
        }

        /// <summary>
        /// Called when agent is activated
        /// </summary>
        public override void OnActivated()
        {
            base.OnActivated();
            this.turretPanMessageProducerAgent.Activate();
        }

        /// <summary>
        /// Called when agent is deactivated
        /// </summary>
        public override void OnDeactivated()
        {
            base.OnDeactivated();
            this.turretPanMessageProducerAgent.Deactivate();
        }

        /// <summary>
        /// Called upon turret position change.
        /// </summary>
        /// <param name="message">Current joint position message.</param>
        public void Receive(SetTurretPanMessage message)
        {
            this.turretPanControlPort.Post(message);
        }
    }
}
